function get_project_select(target_div, name_like) {
  var element = document.getElementById(target_div);
  element.innerHTML="<option value=-1>无选项</option>";
  
  if(name_like=='') {
    return;
  }

  var xmlhttp = new XMLHttpRequest();
  
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      if(xmlhttp.responseText.length > 1) {
        element.innerHTML=xmlhttp.responseText;
      }
    }
  }
  var request = "ajax_response.php?query_project&name_like="+name_like;
  xmlhttp.open("GET", request, true);
  xmlhttp.send();
}

function show_div(div_id, display) {
  document.getElementById(div_id).style.display=display?display:"";
}

function hide_div(div_id) {
  document.getElementById(div_id).style.display="none";
}

function switch_disp(display_id, disappear_id) {
  document.getElementById(disappear_id).style.display="none";
  document.getElementById(display_id).style.display="";
}

function submit_a_value(name, value, action) {
  var f = document.createElement("form");
  f.method = "post";
  f.action = "";
  if(3==arguments.length) {
    f.action = action;
  }
  var i = document.createElement("input");
  i.type="hidden";
  i.name=name;
  i.value=value;
  f.appendChild(i);
  f.submit();
}

function get_wlog_modify_form(wlog_id, div_name, back_div_name) {
  var xmlhttp = new XMLHttpRequest();
  var head_str = '<h2>修改工作记录&nbsp&nbsp<a href=javascript:switch_disp("'
    +back_div_name+'","'+div_name+'");>取消</a></h2>';
  document.getElementById(div_name).innerHTML=head_str+'请稍候...';

  xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById(div_name).innerHTML=head_str+xmlhttp.responseText;
    }
  }
  var request = "ajax_response.php?modify_wlog_form_id="+wlog_id;
  xmlhttp.open("GET", request, true);
  xmlhttp.send();
}

function get_task_modify_form(task_id, div_name, back_div_name) {
  var xmlhttp = new XMLHttpRequest();
  var head_str = '<h2>修改任务&nbsp&nbsp<a href=javascript:switch_disp("'
    + back_div_name + '","'+div_name+'");>取消</a></h2>';
  document.getElementById(div_name).innerHTML=head_str+'请稍候...';
  
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById(div_name).innerHTML = head_str+xmlhttp.responseText;
    }
  }
  var request = "ajax_response.php?modify_task_form_id="+task_id;
  xmlhttp.open("GET", request, true);
  xmlhttp.send();
}

function trim_cr_lf_header(str) {
    var new_str = str;
    if (new_str.length >= 2 && new_str[0] == '\r' && new_str[1] == '\n') {
        new_str = new_str.substr(2);
    } else if (new_str.length >= 1 && (new_str[0] == '\r' || new_str[0] == '\n')) {
        new_str = new_str.substr(1);
    }
    return new_str;
}

function replace_cr_lf_to_br(str) {
    var new_str = str;
    new_str = new_str.replace(/\r\n/g,"<br>");
    new_str = new_str.replace(/\r/g,"<br>");
    new_str = new_str.replace(/\n/g,"<br>");
    return new_str;
}

function switch_outline_td_disp(div_id, task_id, disp_flag) {
    var outline_div_id = "outline_div_id_" + task_id;
    var outline_edit_div_id = "outline_edit_div_id_" + task_id;
    var outline_comment_div_id = "outline_comment_div_id_" + task_id;
    var outline_comment_edit_div_id = "outline_comment_edit_div_id_" + task_id;
    var outline_upd_btn_div_id = "outline_upd_btn_div_id_" + task_id;

    var outline_edit_textarea_id = 'outline_edit_textarea_id_' + task_id;
    var outline_comment_edit_textarea_id = 'outline_comment_edit_textarea_id_' + task_id;
    
    if (disp_flag == 1) {
        if (div_id == outline_upd_btn_div_id 
            && document.getElementById(outline_edit_div_id).className == 'hide_outline_edit_div'
            && document.getElementById(outline_comment_edit_div_id).className == 'hide_outline_comment_edit_div') {
            document.getElementById(div_id).className = "show_outline_upd_btn_div";
        } else if (div_id == outline_div_id) {
            document.getElementById(outline_div_id).className = 'show_outline_div';
            document.getElementById(outline_edit_textarea_id).value = '';
            document.getElementById(outline_edit_div_id).className = 'hide_outline_edit_div';
        } else if (div_id == outline_edit_div_id) {
            var value = document.getElementById(outline_div_id).innerHTML.replace(/<br>/gi,"\n");
            document.getElementById(outline_edit_textarea_id).value = value;
            document.getElementById(outline_edit_div_id).className = 'show_outline_edit_div';
            document.getElementById(outline_div_id).className = 'hide_outline_div';
        } else if (div_id == outline_comment_div_id) {
            document.getElementById(outline_comment_div_id).className = 'show_outline_comment_div';
            document.getElementById(outline_comment_edit_textarea_id).value = '';
            document.getElementById(outline_comment_edit_div_id).className = 'hide_outline_comment_edit_div';
        } else if (div_id == outline_comment_edit_div_id) {
            var value = document.getElementById(outline_comment_div_id).innerHTML.replace(/<br>/gi,"\n");
            document.getElementById(outline_comment_edit_textarea_id).value = value;
            document.getElementById(outline_comment_edit_div_id).className = 'show_outline_comment_edit_div';
            document.getElementById(outline_comment_div_id).className = 'hide_outline_comment_div';
        }
    } else {
        if (div_id == outline_upd_btn_div_id ) {
            document.getElementById(div_id).className = "hide_outline_upd_btn_div";
        } else if (div_id == outline_edit_div_id) {
            switch_outline_td_disp(outline_div_id, task_id, 1);
        } else if (div_id == outline_comment_edit_div_id) {
            switch_outline_td_disp(outline_comment_div_id, task_id, 1);
        }
    }
}

function save_outline(year, week, task_id, textarea_id, outline_div_id) {
    update_work_outline('outline', year, week, task_id, document.getElementById(textarea_id).value, outline_div_id);
}

function save_outline_comment(year, week, task_id, textarea_id, outline_comment_div_id) {
    update_work_outline('outline_comment', year, week, task_id, document.getElementById(textarea_id).value, outline_comment_div_id);
}

function update_work_outline(type, year, week, task_id, text, disp_div_id) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById(disp_div_id).innerHTML = trim_cr_lf_header(xmlhttp.responseText);
          switch_outline_td_disp(disp_div_id, task_id, 1);
      }
    }
    var req_str = 'update_work_outline_type=' + type + '&year=' + year + '&week=' + week + '&task_id=' + task_id + '&text=' + text;
    xmlhttp.open("POST", "ajax_response.php", true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send(req_str);
}

function switch_plan_td_disp(div_id, task_id, disp_flag) {
    var plan_div_id = "plan_div_id_" + task_id;
    var plan_edit_div_id = "plan_edit_div_id_" + task_id;
    var plan_comment_div_id = "plan_comment_div_id_" + task_id;
    var plan_comment_edit_div_id = "plan_comment_edit_div_id_" + task_id;
    var plan_upd_btn_div_id = "plan_upd_btn_div_id_" + task_id;

    var plan_edit_textarea_id = 'plan_edit_textarea_id_' + task_id;
    var plan_comment_edit_textarea_id = 'plan_comment_edit_textarea_id_' + task_id;
    
    if (disp_flag == 1) {
        if (div_id == plan_upd_btn_div_id 
            && document.getElementById(plan_edit_div_id).className == 'hide_plan_edit_div'
            && document.getElementById(plan_comment_edit_div_id).className == 'hide_plan_comment_edit_div') {
            document.getElementById(div_id).className = "show_plan_upd_btn_div";
        } else if (div_id == plan_div_id) {
            document.getElementById(plan_div_id).className = 'show_plan_div';
            document.getElementById(plan_edit_textarea_id).value = '';
            document.getElementById(plan_edit_div_id).className = 'hide_plan_edit_div';
        } else if (div_id == plan_edit_div_id) {
            var value = document.getElementById(plan_div_id).innerHTML.replace(/<br>/gi,"\n");
            document.getElementById(plan_edit_textarea_id).value = value;
            document.getElementById(plan_edit_div_id).className = 'show_plan_edit_div';
            document.getElementById(plan_div_id).className = 'hide_plan_div';
        } else if (div_id == plan_comment_div_id) {
            document.getElementById(plan_comment_div_id).className = 'show_plan_comment_div';
            document.getElementById(plan_comment_edit_textarea_id).value = '';
            document.getElementById(plan_comment_edit_div_id).className = 'hide_plan_comment_edit_div';
        } else if (div_id == plan_comment_edit_div_id) {
            var value = document.getElementById(plan_comment_div_id).innerHTML.replace(/<br>/gi,"\n");
            document.getElementById(plan_comment_edit_textarea_id).value = value;
            document.getElementById(plan_comment_edit_div_id).className = 'show_plan_comment_edit_div';
            document.getElementById(plan_comment_div_id).className = 'hide_plan_comment_div';
        }
    } else {
        if (div_id == plan_upd_btn_div_id ) {
            document.getElementById(div_id).className = "hide_plan_upd_btn_div";
        } else if (div_id == plan_edit_div_id) {
            switch_plan_td_disp(plan_div_id, task_id, 1);
        } else if (div_id == plan_comment_edit_div_id) {
            switch_plan_td_disp(plan_comment_div_id, task_id, 1);
        }
    }
}

function save_plan(year, week, task_id, textarea_id, plan_div_id) {
    update_work_plan('plan', year, week, task_id, document.getElementById(textarea_id).value, plan_div_id);
}

function save_plan_comment(year, week, task_id, textarea_id, plan_comment_div_id) {
    update_work_plan('plan_comment', year, week, task_id, document.getElementById(textarea_id).value, plan_comment_div_id);
}

function update_work_plan(type, year, week, task_id, text, disp_div_id) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById(disp_div_id).innerHTML = trim_cr_lf_header(xmlhttp.responseText);
          switch_plan_td_disp(disp_div_id, task_id, 1);
      }
    }

    var req_str = 'update_work_plan_type=' + type + '&year=' + year + '&week=' + week + '&task_id=' + task_id + '&text=' + text;
    xmlhttp.open("POST", "ajax_response.php", true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send(req_str);
}

function switch_the_disp(id) {
    if (document.getElementById(id).style.display == 'none') {
        document.getElementById(id).style.display = 'block';
    } else {
        document.getElementById(id).style.display = 'none';
    }
}

function get_elements_by_class_name(className, tagName) {
    if (arguments.length < 1) {
        return;
    }
    if (arguments.length < 2) {
        tagName = '*';
    }
    var elements = new Array();
    var tags = document.getElementsByTagName(tagName);
    for (i=0; i<tags.length; ++i) {
        if (tags[i].className == className) {
            elements[elements.length] = tags[i];
        }
    }
    return elements;
}

function switch_all_rele_plan_div_disp() {
    var all_rele_plan_div = get_elements_by_class_name('rele_plan_div');
    var display;
    for (i=0; i<all_rele_plan_div.length; ++i) {
        if (i == 0) {
            if (all_rele_plan_div[i].style.display == 'none') {
                display = 'block';
            } else {
                display = 'none';
            }
        }
        all_rele_plan_div[i].style.display = display;
    }
}
